//
//  YUYAMeViewController.m
//  com.yigu.yuya
//
//  Created by hxp on 16/3/2.
//  Copyright © 2016年 hxp. All rights reserved.
// 新消息通知界面

#import "YAYINotifySettingViewController.h"

#import "YAYISettingSectionModel.h"
#import "YAYISettingItemModel.h"

#import "YAYINotifyCell.h"

#import <AVFoundation/AVFoundation.h>


#define YAYIShockSwitch 400
#define YAYIVoiceSwitch 401


@interface YAYINotifySettingViewController ()

@property (nonatomic, weak) UIView *footerView;
@property (strong,nonatomic) UIButton *backBtn;

@end

@implementation YAYINotifySettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //YAYIMapView *mapView = [[YAYIMapView  alloc] initWithFrame:CGRectMake(0, 0, SCREEN_width, SCREEN_height)];
    //[self.view addSubview:mapView];
    
    
    self.title = @"新消息通知";

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.separatorInset = UIEdgeInsetsZero;
    [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    [self.tableView setSectionHeaderHeight:0];
    
    self.tableView.separatorColor = YAYICellLineColor;

    // 添加组
    [self setupSection0];
//    [self setupSection1];

}


- (void)setupSection0
{
    
    NSString *subTitle = nil;
    UIUserNotificationType notifyType= [[UIApplication sharedApplication] currentUserNotificationSettings].types;
    if (UIUserNotificationTypeNone == notifyType) {
        subTitle = @"未开启";
    }else{
        subTitle = @"已开启";
    }
    
    
    YAYISettingItemModel *item0 = [YAYISettingItemModel itemWithIcon:nil name:@"接受新消息提示" detail:subTitle];
//    item0.isHaveArrow = NO;
    item0.executeOperation = ^(NSIndexPath *indexPath){
        YAYILog(@"密码修改");
    };
    
    YAYISettingSectionModel *section0 = [[YAYISettingSectionModel alloc]init];
    section0.itemArray = @[item0];
    
    [self.sectionArray addObject:section0];
    
    if (UIUserNotificationTypeNone == notifyType) {
        
    }else{
        [self setupSection1];
    }
}


- (void)setupSection1
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    YAYISettingItemModel *item0 = [YAYISettingItemModel itemWithIcon:nil name:@"声音"];
    item0.isOn = [userDefaults boolForKey:YAYIVoiceID];
    
    item0.executeOperation = ^(NSIndexPath *indexPath){
        YAYILog(@"新消息通知");
    };
    
    YAYISettingItemModel *item1 = [YAYISettingItemModel itemWithIcon:nil name:@"震动"];
    item1.isOn = [userDefaults boolForKey:YAYIShockID];
    item1.executeOperation = ^(NSIndexPath *indexPath){
        YAYILog(@"新消息通知");
    };
    
    
    YAYISettingSectionModel *section1 = [[YAYISettingSectionModel alloc]init];
    section1.itemArray = @[item0, item1];
    
    [self.sectionArray addObject:section1];
}


#pragma mark - 按钮相应方法

- (void)quitClick:(UIButton *)sender
{
    YAYILog(@"quitClick");
}


#pragma mark - 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    NSArray *strArray = @[@"您可以在iPhone的“设置-通知中心”找到应用程序“雅同”，更改雅同的新消息提醒设置。",
                          @"当雅同运行时，您可以设置提醒形式：声音或者震动。"
                          ];
    NSString *str = strArray[section];
    CGSize strSize = [str sizewithFont:[UIFont systemFontOfSize:16] maxSize:CGSizeMake(SCREEN_width - 20 * 2, MAXFLOAT)];
    
    return strSize.height + 15 * 2;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc] init];
    
    UILabel *lbl = [[UILabel alloc] init];
    lbl.font = [UIFont systemFontOfSize:16];
    lbl.textColor = YAYIColor(146, 146, 146);
    lbl.numberOfLines = 0;
    [footerView addSubview:lbl];
    [lbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(footerView).offset(20);
        make.right.equalTo(footerView).offset(-20);
        make.centerY.equalTo(footerView);
    }];

    if (0 == section) {
        lbl.text = @"您可以在iPhone的“设置-通知中心”找到应用程序“雅同”，更改雅同的新消息提醒设置。";
    }else if(1 == section){
        lbl.text = @"当雅同运行时，您可以设置提醒形式：声音或者震动。";
    }
    
    CGSize strSize = [lbl.text sizewithFont:[UIFont systemFontOfSize:16] maxSize:CGSizeMake(SCREEN_width - 20 * 2, MAXFLOAT)];
    
    footerView.frame = CGRectMake(0, 0, SCREEN_width, strSize.height + 2 * 15);
    
    return footerView;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 去除数据模型
    YAYISettingSectionModel *section = self.sectionArray[indexPath.section];
    YAYISettingItemModel *model = section.itemArray[indexPath.row];

    
    YAYINotifyCell *cell = [YAYINotifyCell cellWith:tableView];
    cell.model = model;
    [cell setLayoutMargins:UIEdgeInsetsZero];
    cell.switchChangeOperation = ^(BOOL isOn){
        [self switchClick:isOn indexPath:indexPath];
    };
    [cell setLayoutMargins:UIEdgeInsetsZero];
    
    return cell;
}

- (void)switchClick:(BOOL)isOn indexPath:(NSIndexPath *)indexPath
{
    YAYILog(@"switchClick:%d", isOn);
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if (0 == indexPath.row) {
        [userDefaults setBool:isOn forKey:YAYIVoiceID];
        
    }else if (1 == indexPath.row){
        [userDefaults setBool:isOn forKey:YAYIShockID];
        
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
